<template>
	<!-- 聊天列表 -->
	<view class="chat-list">
		<!-- 时间分隔 -->
		<view v-if="formatTime" class="time-separator">
			{{ formatTime }}
		</view>

		<!-- 消息容器 -->
		
		<view :class="['message-wrapper', { self: isSelf }]">
			<!-- 头像 -->
			<image class="user-img" :src="item.sendAvatar" mode="aspectFill" @click="goToDetail(item.senderId)"></image>
			<!-- 消息气泡 -->
			<view class="chat-bubble">
				{{ item.content }}
			</view>
		</view>
	</view>
</template>

	<script>
	import $time from '@/utils/lib/time.js'

	export default {
		props: {
			item: Object, // 当前消息
			pretime: [Number, String], // 上一条消息时间
		},
		data() {
			return {
				
			}
		},
		computed: {
			isSelf() {
				const sendId = this.item.senderId
				const userId = getApp().globalData.userId
				return sendId !== undefined && sendId !== null && sendId === userId
			},
			// 格式化时间
			formatTime() {
				return $time.getChatTime(
					new Date(this.item.createTime).getTime(),
					new Date(this.pretime).getTime()
				)
			}
		},
		methods: {
			// 新增方法：点击头像跳转到好友详情页
			goToDetail(userId) {
				const currentUserId = getApp().globalData.userId;
					
				// 判断是否是自己
				if (userId === currentUserId) {
					uni.navigateTo({
						url: '/pages/mine/user-space'
					});
				}else{
					uni.navigateTo({
						url: `/pages/paper/friend_profile?userId=${userId}`
					});
				}
			}
		}
	}
	</script>

<style lang="scss" scoped>
.chat-list {
	// 移除了 align-items: center，避免整体居中
	display: flex;
	flex-direction: column;
	width: 100%;
	padding: 0 20rpx;
	box-sizing: border-box;
}

.time-separator {
	font-size: 24rpx;
	color: #999;
	margin: 20rpx 0;
	text-align: center;
	width: 100%;
}

.message-wrapper {
	display: flex;
	align-items: start;
	margin: 20rpx 0;
	max-width: 70%;
	width: auto;

	.user-img {
		width: 80rpx;
		height: 80rpx;
		border-radius: 40rpx;
		flex-shrink: 0;
	}

	.chat-bubble {
		max-width: 500rpx;
		padding: 20rpx;
		background-color: #f0f0f0;
		border-radius: 16rpx;
		margin-left: 20rpx;
		font-size: 28rpx;
		word-break: break-word;
	}

	&.self {
		flex-direction: row-reverse;
		align-self: flex-end; // 关键：将自己发送的消息靠右对齐

		.chat-bubble {
			background-color: #07c160;
			color: white;
			margin-left: 0;
			margin-right: 20rpx;
		}
	}
}
</style>